<?php

class Task extends BaseModel 
{
	/**
     * @var int task id
     * @soap
     */
    public $task_id;
    
    /**
     * @var string base date
     * @soap
     */
    public $date;

    /**
     * @var int task type
     * @soap
     */
    public $task_type;
    
    /**
     * @var int task frequency
     * @soap
     */
    public $frequency;
    
     /**
     * @var int task priority
     * @soap
     */
    public $priority;
    
    /**
     * @var string task start time
     * @soap
     */
    public $start_time;
    
    /**
     * @var string task end time
     * @soap
     */
    public $end_time;
    
     /**
     * @var int task state
     * @soap
     */
    public $state;
    
     /**
     * @var string task detail
     * @soap
     */
    public $detail;
    
    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'task';
    }

    public function relations() {
        return array(
            'job'  =>  array(self::BELONGS_TO, 'Job', 'job_id'),
            'command' =>  array(self::BELONGS_TO, 'Command', 'cmd_id'),
        	'commands'	=>	array(self::HAS_MANY, 'Command', 'cmd_id'),
            'executor_machine' =>  array(self::BELONGS_TO, 'ExecutorMachine', 'executor_id'),
        );
    }
    
    /**
     *
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'task_id' => '任务序号',
            'date' => '基准时间',
            'task_type' => '任务类型',
            'priority' => '优先级别',
            'frequency' => '运行周期',
            'start_time' => '开始时间',
            'end_time' => '结束时间',
            'submit_time' => '提交时间',
            'executor_id'=>'执行机器',
            'runtime_info'   => '运行时信息',
            'detail'   => '详细信息',
            'state' => '任务状态'
        );
    }


    public function getControlLink($model)
    {
        if($model->state == TaskState::Done) {
            return CHtml::linkButton('重新运行', array(
            	'submit'=>array('task/rerun','id'=>$model->task_id),
            	'confirm'=>'确认重新运行该任务?',
            ));
        }
        elseif($model->state == TaskState::Failed) {
            return CHtml::linkButton('恢复执行', array( 
                'submit'=>array('task/resume','id'=>$model->task_id), 
                'confirm'=>'确认恢复执行该任务?',
            ));
        }
        elseif(in_array($model->state, array(TaskState::Running, TaskState::Waiting, TaskState::Ready))) {
            return CHtml::linkButton('杀死任务', array( 
                'submit'=>array('task/kill','id'=>$model->task_id),
                'confirm'=>'确认杀死该任务?',
            ));
        }
    }

    public function getCommands($taskId) {
        $criteria=new CDbCriteria;
        $criteria->compare('task_id', $taskId);
        
        return new CActiveDataProvider('Command', array(
            'criteria'=>$criteria,
            'sort'=>array(
                'defaultOrder'=>'cmd_id DESC',
            ),
        ));
    }

    public function formatRuntimeInfo($runtimeInfo) 
    {
        $formatMsg = '';
        $results = CJSON::decode($runtimeInfo);
        if(is_array($results)) {
            foreach($results['cost'] as $key => $result) {

                $formatMsg .= "$key cost {$result} seconds<br/>";
            }
        }
        return $formatMsg;
    }

    /**
     * @return string the URL that shows the result of the task
     */
    public function getPartitionUrl($partition)
    {
        return Yii::app()->createUrl('resource/view', array(
            'id'=>$partition,
        ));
    }
    
    /**
     * @return string the URL that shows the detail of the task
     */
    public function getUrl()
    {
        return Yii::app()->createUrl('task/view', array(
            'id'=>$this->task_id,
        ));
    }
}
